<template>
    <div>
        <Row>
            <Form :label-width="70" :model="queryParams" class="search-form" inline ref="searchForm">
                <Form-item label="执行器" prop="jobGroup">
                    <Select :clearable="true" style="width:200px" v-model="queryParams.jobGroup"  @on-change="groupChange">
                        <Option :key="item.id" :value="item.id" v-for="item in executors">{{ item.title }}</Option>
                    </Select>
                </Form-item>
                <Form-item label="作业" prop="jobGroup">
                    <Select :clearable="true" style="width:200px" v-model="queryParams.jobId">
                        <Option :key="item.id" :value="item.id" v-for="item in jobs">{{ item.jobDesc }}</Option>
                    </Select>
                </Form-item>
                <Form-item label="状态" prop="jobGroup">
                    <Select :clearable="true" style="width:200px" v-model="queryParams.logStatus">
                        <Option :key="item.code" :value="item.code" v-for="item in statusEnum">{{ item.msg }}</Option>
                    </Select>
                </Form-item>

                <Form-item label="日志时间" prop="executorHandler">
                    <DatePicker  :transfer="true" clearable format="yyyy-MM-dd" placeholder="选择起始时间" type="daterange"
                                v-model="queryParams.filterTime"/>&nbsp;&nbsp;&nbsp;
                </Form-item>

                <Form-item class="br" style="margin-left:-35px;">
                    <Button @click="getList" icon="ios-search" type="primary">搜索</Button>
                    <Button @click="clearJobLogs">清除日志</Button>
                </Form-item>
            </Form>
        </Row>
        <br/>
        <Row>
            <Table :columns="columns" :data="list" :loading="listLoading" border ref="loglist">
                <template slot="action" slot-scope="{ row}">
                    <Button style="margin-right:5px" @click="jobLog(row)"  type="primary">日志详情</Button>

                    <Button @click="stopJob(row.id)" type="error" v-show="row.triggerCode==200 && row.handleCode==0">终止
                    </Button>
                </template>
                <template slot="triggerCode" slot-scope="{ row}">
                    <span v-show="row.triggerCode==500" style="color: red">调度失败</span>
                    <span v-show="row.triggerCode==502" style="color: red">调度超时</span>
                    <span v-show="row.triggerCode==200" style="color: green">调度成功</span>
                    <span v-show="row.triggerCode==0" style="color: #ffb81c">未知结果</span>
                </template>
                <template slot="handleCode" slot-scope="{ row}">
                    <span v-if="row.handleMsg!=null&&row.handleMsg!=''">{{row.handleMsg}}</span>
                    <div v-else>
                        <span v-show="row.handleCode==500" style="color: red">执行失败</span>
                        <span v-show="row.handleCode==502" style="color: red">执行超时</span>
                        <span v-show="row.handleCode==200" style="color: green">执行成功</span>
                        <span v-show="row.handleCode==0" style="color: #ffb81c">未知结果</span>
                    </div>
                </template>
            </Table>
        </Row>
        <br>
        <Row class="page" justify="end" type="flex">
            <Page :current="queryParams.pageNum" :page-size="queryParams.pageSize" :page-size-opts="[10,20,50]"
                  :total="total" @on-change="handleCurrentChange" @on-page-size-change="handleSizeChange"
                  show-elevator show-sizer show-total size="small"></Page>
        </Row>
        <Modal :mask-closable='false' :styles="{top: '90px'}" title="清除执行器日志"
               :width="600"
               v-model="clearLogDialog">
            <Form :label-width="80" :model="clearForm" :rules="rules" ref="clearForm">
                <Form-item  label="执行器" prop="jobGroup">
                    <Select :clearable="true" v-model="clearForm.jobGroup"  @on-change="groupClearChange">
                        <Option :key="item.id" :value="item.id" v-for="item in executors">{{ item.title }}</Option>
                    </Select>
                </Form-item>
                <Form-item label="作业名称" prop="jobId">
                    <Select v-model="clearForm.jobId" placeholder="请选择作业">
                        <Option v-for="item in clearJobs" :key="item.id" :value="item.id">
                            {{ item.jobDesc }}
                        </Option>
                    </Select>
                </Form-item>
                <Form-item label="时间段" prop="type">
                    <Select v-model="clearForm.type" placeholder="请选择时间段">
                        <Option v-for="item in logTimeEnum" :key="item.code" :value="item.code">
                            {{ item.msg }}
                        </Option>
                    </Select>
                </Form-item>
            </Form>
            <div slot="footer" class="dialog-footer">
                <Button @click="resetClearForm">取 消</Button>
                <Button type="primary" @click="exeClearLogs">确 定</Button>
            </div>
        </Modal>
        <Modal :mask-closable='false' :styles="{top: '90px'}" title="调度详情"
               :width="600"
               v-model="dialogFormVisible">
            <div v-html="jobLogDetail"></div>
            <div slot="footer" class="dialog-footer">
                <Button type="primary" @click="dialogFormVisible = false">确 定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import {listAllGroup} from "@/projects/basic/api/jobadmin/group";
    import {clearLogs, getLogsList, stopJob} from "@/projects/basic/api/jobadmin/logs";
    import {getJobsByGroup} from "@/projects/basic/api/jobadmin/jobs";
    import  util  from '@/libs/util'
    export default {
        props: {},
        components: {},
        data() {
            return {
                columns: [
                    {
                        title: "任务ID",
                        key: 'jobId',
                        width: 80,
                        align: "center"
                    }
                    , {
                        title: "任务执行器",
                        key: 'executorHandler',
                        tooltip: true,
                        align: "center"
                    }
                    , {
                        title: "调度时间",
                        key: 'triggerTime',
                        tooltip: true,
                        align: "center"
                    }
                    , {
                        title: "调度结果",
                        key: 'triggerCode',
                        slot:'triggerCode',
                        align: "center"
                    }
                    , {
                        title: "执行时间",
                        key: 'handleTime',
                        tooltip: true,
                        align: "center"
                    }, {
                        title: "执行结果",
                        key: 'handleCode',
                        slot:'handleCode',
                        align: "center"
                    }, {
                        title: "操作",
                        align: "center",
                        width:180,
                        slot: 'action'
                    }

                ],
                statusEnum: [{code: 1, msg: '成功'}, {code: 2, msg: '失败'}, {code: 3, msg: '进行中'}],
                executors: [],
                jobs: [],
                clearJobs: [],
                dialogStatus: 'create',
                dialogFormVisible: false,
                clearLogDialog: false,
                headers: {},
                modifyData: '',
                list: [],
                total: 0,
                listLoading: false,
                queryParams: {
                    jobGroup: '',
                    jobId: '',
                    pageNum: 1,
                    pageSize: 10,
                    logStatus: '',
                    filterTime: [],
                    triggerTimeStart:'',
                    triggerTimeEnd:''
                },
                tableKey: 0,
                jobLogDetail:'',
                logTimeEnum: [{code: 1, msg: '一个月前'}, {code: 2, msg: '三个月前'}, {code: 3, msg: '六个月前'}, {
                    code: 4,
                    msg: '一年之前'
                }
                , {code: 5, msg: '清理一千条以前日志数据'}
                , {code: 6, msg: '清理一万条以前日志数据'}
                , {code: 7, msg: '清理三万条以前日志数据'}
                , {code: 8, msg: '清理十万条以前日志数据'}
                , {code: 9, msg: '清理所有日志数据'}
                ],
                clearForm: {
                    jobGroup: '',
                    jobId: '',
                    type: ''
                },
                rules: {
                    jobGroup: [{required: true, message: '请选择执行器', trigger: 'blur',type:'number'}],
                    jobId: [{required: true, message: '请选择作业', trigger: 'blur',type:'number'}],
                    type: [{required: true, message: '请选择时间段', trigger: 'blur',type:'number'}]
                },
            }
        },
        created() {
        },
        mounted() {
            this.getExecutors()
            this.getList()
        },
        watch: {},
        methods: {
            getList() {
                if(this.queryParams.filterTime.length==0){
                    this.queryParams.triggerTimeStart = '';
                    this.queryParams.triggerTimeEnd = '';
                }else{
                    if(this.queryParams.filterTime[0]!=''){
                        this.queryParams.triggerTimeStart = util.formatDate(this.queryParams.filterTime[0]);
                    }
                    if(this.queryParams.filterTime[1]!=''){
                        this.queryParams.triggerTimeEnd = util.formatDate(this.queryParams.filterTime[1]);
                    }
                }
                getLogsList(this.queryParams).then(res => {
                    const {code, data: {records, total}} = res
                    this.listLoading = false
                    if (code === '0000') {
                        this.list = records
                        this.total = total
                    } else {
                        this.$Message.error(res.msg)
                    }
                }).catch(function (err) {
                    console.log(err)
                })
            },
            getExecutors() { //查询执行器
                listAllGroup().then(res => {
                    const {code, data} = res
                    if (code === '0000') {
                        this.executors = data
                    }
                }).catch(function (err) {
                    console.log(err.message)
                })
            },
            groupChange() {
                this.queryParams.jobId = ''
                if (this.queryParams.jobGroup !== '') {
                    getJobsByGroup(this.queryParams.jobGroup).then(res => {
                        const {code, data} = res
                        if (code === '0000') {
                            this.jobs = data
                        }
                    }).catch(function (err) {
                        console.log(err.message)
                    })
                }
            },
            groupClearChange() {
                this.clearForm.jobId = ''
                if (this.clearForm.jobGroup&&this.clearForm.jobGroup != '') {
                    getJobsByGroup(this.clearForm.jobGroup).then(res => {
                        const {code, data} = res
                        if (code =='0000') {
                            this.clearJobs = data
                        }
                    }).catch(function (err) {
                        console.log(err.message)
                    })
                }
            },
            resetClearForm(){
                this.clearLogDialog = false;
                this.$refs.clearForm.resetFields();
            },
            stopJob(id) {
                this.$Modal.confirm({
                    title: "确认",
                    content: '确认终止此次调度作业？',
                    onOk: () => {
                        stopJob(id).then(res => {
                            const {code, msg} = res
                            if (code === '0000') {
                                this.$Message.success('终止成功')
                            } else {
                                this.$Message.warning('终止失败')
                            }
                            this.getList();
                        }).catch(function (err) {
                            console.log(err.message)
                        })
                    }
                });
            },
            jobLog(row) {
                this.jobLogDetail = row.triggerMsg
                this.dialogFormVisible = true;
            },
            clearJobLogs() {
                this.clearLogDialog = true
            },
            exeClearLogs() {
                this.$refs['clearForm'].validate((valid) => {
                    if (valid) {
                        clearLogs(this.clearForm).then(res => {
                            if (res.code === '0000') {
                                this.$Message.success('清除成功')
                                this.clearLogDialog = false
                                this.$refs.clearForm.resetFields();
                                this.getList();
                            } else {
                                this.$Message.error('清除失败')
                            }
                        }).catch(function (err) {
                            console.log(err.message())
                        })
                    }
                })
            },
            handleFilter() {
                this.queryParams.start = 1
                this.getList()
            }
            ,
            handleSizeChange(val) {
                this.queryParams.pageSize = val
                this.getList()
            }
            ,
            handleCurrentChange(val) {
                this.queryParams.pageNum = val
                this.getList()
            }
        }
    }
</script>
